package com.idp.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.idp.common.base.BaseController;
import com.idp.common.model.LoginInfo;
import com.idp.common.model.ResultModel;
import com.idp.common.token.service.TokenService;
import com.idp.system.dto.SysButtonDto;
import com.idp.system.dto.SysUserDto;
import com.idp.system.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 登录Controller
 * @author king
 */
@Api(tags="登录")
@RestController
@RequestMapping("/login/")
public class LoginController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private LoginService loginService;
	@Autowired
	private TokenService tokenService;

	/**
	 * 登录
	 * @param loginParams
	 * @return
	 */
	@ApiOperation("登录")
	@DynamicParameters(name = "loginParams", properties = {
			@DynamicParameter(name = "uname", value = "用户名", dataTypeClass = String.class, required = true),
			@DynamicParameter(name = "pwd", value = "密码", dataTypeClass = String.class, required = true)
	})
	@PostMapping("login")
	public ResultModel<LoginInfo> login(@RequestBody Map<String, Object> loginParams){
		SysUserDto user = BeanUtil.toBean(loginParams, SysUserDto.class);
		LoginInfo loginInfo = loginService.login(user);
		// 清空菜单权限缓存
		loginService.deleteRuleMenusCache(loginInfo.getId());
		// 清空按钮权限缓存
		loginService.deleteRuleButtonsCache(loginInfo.getId());
		log.info(user.getUname() + ",登录成功");
		return ResultModel.success(loginInfo);
	}

	/**
	 * 获取权限菜单
	 * @return
	 */
	@ApiOperation("获取权限菜单")
	@PostMapping("getAuthMenus")
	public ResultModel getAuthMenus() {
		LoginInfo loginInfo = tokenService.getLoginInfo();
		return ResultModel.success(loginService.getLoginRuleMenus(loginInfo.getId()));
	}

	/**
	 * 校验按钮权限
	 * @param buttonCode
	 * @return
	 */
	@ApiOperation("校验按钮权限")
	@ApiImplicitParam(name = "buttonCode", value = "按钮编码", paramType = "body", dataTypeClass = String.class, required = true)
	@PostMapping("checkAuthButton")
	public ResultModel<Boolean> checkAuthButton(@RequestBody String buttonCode) {
		log.info("按钮标识：" + buttonCode);
		if(StrUtil.isNotEmpty(buttonCode)) {
			LoginInfo loginInfo = tokenService.getLoginInfo();
			List<SysButtonDto> ruleButtons = loginService.getLoginRuleButtons(loginInfo.getId());
			if(ruleButtons != null && !ruleButtons.isEmpty()) {
				for(SysButtonDto button : ruleButtons) {
					if(buttonCode.equals(button.getBtnCode())) {
						return ResultModel.success(true);
					}
				}
			}
		}
		return ResultModel.success(false);
	}
}
